<template>
  <div>
    <el-button
      type="primary"
      @click="dialogFormVisible = true"
      style="margin-top: 20px"
      icon="el-icon-edit"
      >添加</el-button
    >
    <el-table
      :data="tableData"
      style="width: 90%"
      row-key="id"
      :row-class-name="tableRowClassName"
      border
      :tree-props="{
        children: 'departmentTableList',
        hasChildren: 'hasChildren',
      }"
    >
      <el-table-column prop="id" label="日期" sortable width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" sortable width="180">
      </el-table-column>
      <el-table-column
        prop="introduce"
        label="地址"
        height="200"
      ></el-table-column>
    </el-table>

    <el-dialog title="添加门诊" :visible.sync="dialogFormVisible">
      <el-form :model="form">

        <el-row class="demo-autocomplete">
          <el-col :span="12">
            <el-autocomplete
            v-on:input="gaibian"
              class="inline-input"
              v-model="state1"
              :fetch-suggestions="querySearch"
              placeholder="请选择科室名称"
              @select="handleSelect"
            ></el-autocomplete>
          </el-col>
        </el-row>

        <div style="margin: 20px 0"></div>
        <el-input placeholder="请输入添加的门诊" v-model="form.name" :disabled="jinyong"></el-input>
        <div style="margin: 20px 0"></div>

        <el-input
         :disabled="jinyong"
          type="textarea"
          :rows="5"
          placeholder="请输入科室介绍"
          v-model="form.introduce"
        >
        </el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="tianjia"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<style>
.el-table .warning-row {
  background: rgb(209, 179, 124);
}

.el-table .success-row {
  background: #98e76e;
}

.radio-label {
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 30px;
}
</style>
<script>
import { getList, getdep } from "../../../../api/Department";
import { addDepTable } from "../../../../api/DepartmentTable";

export default {
  data() {
    return {
      tableData: [],
      form: {
        name: "",
        ksid: "",
        suoshuyuanqu:1,
        introduce:""

      },
      jinyong:true,
      options: [],
      value: "",
      state1: "",
      input:'',
      restaurants: [],
      dialogFormVisible: false,
      textarea: "",
    };
  },
  methods: {
    tianjia(){
        console.log(this.form)
        this.dialogFormVisible=false;
        addDepTable(this.form).then((res) => {
        if (res.success) {
          this.Lodwn();
          this.form = {};
          this.$message({
            message: "添加成功", 
            type: "success",
          });
        } else {
          this.$message.error("添加失败");
        }
      });


        
    },
    gaibian(){
      
       console.log("d")
       if(this.state1 != "" ){
          this.jinyong=false
       }else{
         this.jinyong=true
       }
       
       
    },
    loadAll() {
      getdep().then((res) => {
        var arr = [];
        for (let index = 0; index < res.result.length; index++) {
          arr.push({
            value: res.result[index].ksName,
            id: res.result[index].id,
          });
        }

        this.restaurants = arr;

        console.log(this.restaurants);
      });
    },
    handleSelect(item) {
      this.form.ksid = item.id;
      console.log(item);
    },
    querySearch(queryString, cb) {
      var restaurants = this.restaurants;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    Lodwn() {
      getList().then((res) => {
        if (res.success) {
          var arr = [];
          for (let index = 0; index < res.result.length; index++) {
            for (
              let index2 = 0;
              index2 < res.result[index].departmentTableList.length;
              index2++
            ) {
              res.result[index].departmentTableList[index2].id =
                " " + (index2 + 1) + " ";
            }
          }
          this.tableData = res.result;
          this.options = res.result;
          console.log(res.result[0]);
        }
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 3) {
      }
      return "";
    },
  },

  mounted() {
    this.Lodwn();
    this.loadAll();
  },
};
</script>